/**
 * Copyright (c) 2016,http://www.365wuliu.com/  All Rights Reserved.
 */
package com.cargo.base.mail;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.util.Date;
import java.util.Properties;

/**
 * Sendmail:发邮件主要类
 * 
 * @Project Name:cargo
 * @File Name:Sendmail.java
 * @Package Name:com.cargo.common.mail
 * @Creator:wizard
 * @Date:2016年4月14日下午2:12:30
 */
public class Sendmail {

	private final static String MIMETYPE = "text/html; charset=gb2312";

	private static MimeMessage build(Mail mail, MimeMessage msg) throws Exception {

		Multipart mPart = new MimeMultipart();
		String ss =  MimeUtility.encodeText(mail.screenname) + "<" + mail.from + ">";

		msg.setFrom(new InternetAddress(MimeUtility.encodeText(mail.screenname) + "<" + mail.from + ">"));
		InternetAddress[] address = InternetAddress.parse(mail.to);
		msg.setRecipients(Message.RecipientType.TO, address);

		if (mail.cc != null) {
			InternetAddress[] ccaddress = InternetAddress.parse(mail.cc);
			msg.setRecipients(Message.RecipientType.CC, ccaddress);
		}
		if (mail.bcc != null) {
			InternetAddress[] bccaddress = InternetAddress.parse(mail.bcc);
			msg.setRecipients(Message.RecipientType.BCC, bccaddress);
		}

		msg.setSubject(mail.subject);

		InternetAddress[] replyAddress = { new InternetAddress(mail.from) };
		msg.setReplyTo(replyAddress);

		MimeBodyPart mBodyContent = new MimeBodyPart();
		mBodyContent.setContent(mail.content, MIMETYPE);
		mPart.addBodyPart(mBodyContent);

		msg.setContent(mPart);
		msg.setSentDate(new Date());
		return msg;
	}

	public static void send(Smtp smtp, Mail mail) throws Exception {
		Transport trans = null;
		try {
			if (smtp.host.indexOf(":") != -1) {
				smtp.host = smtp.host.substring(0, smtp.host.indexOf(":"));
				smtp.port = smtp.host.substring(smtp.host.indexOf(":") + 1);
			}

			Properties props = System.getProperties();
			// props.put("mail.smtp.host", smtp.host);
			// props.put("mail.smtp.port", smtp.port);
			// props.put("mail.smtp.auth", "true");
			// props.put("mail.smtp.timeout", "30000");
			// props.put("mail.smtp.starttls.enable", "true");
			// MailAuthenticator auth = new MailAuthenticator(smtp.username,
			// smtp.password);
			// Session session = Session.getInstance(props, auth);

			props.setProperty("mail.host", smtp.host);
			props.setProperty("mail.transport.protocol", "smtp");
			props.setProperty("mail.smtp.auth", "true");
			Session session = Session.getInstance(props);
			trans = session.getTransport();

			MimeMessage msg = new MimeMessage(session);

			build(mail, msg);
			trans.connect(smtp.host, smtp.username, smtp.password);
			trans.sendMessage(msg, msg.getAllRecipients());
			trans.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (trans != null)
				trans.close();
		}
	}

}
